package com.beaven.imserver.net.utils;

import com.beaven.imserver.net.providers.GsonProvider;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * author: Beaven
 * time: 2017/5/17 17:03
 */
public class TextUtil {

  private TextUtil() {
  }

  // 计算字符串MD5值
  public static String getMD5(String str) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(str.getBytes());
      return new BigInteger(1, md.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return str;
    }
  }

  // 对字符串进行Base64编码
  public static String encodeBase64(String str) {
    return Base64.getEncoder().encodeToString(str.getBytes());
  }

  // 把Object转换为Json字符串
  public static String toJson(Object obj) {
    return GsonProvider.getGson().toJson(obj);
  }

  public static int getRandomNumber(int start, int end) {
    return (int) (Math.random() * (end - start + 1)) + start;
  }
}
